package com.liy.framework.common.orm.transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;

/**
* @Author:  liyang
* @Date:    2019/11/24  16:19
* @Description: 事务拦截器配置
*
**/
@Aspect
@Configuration
public class TransactionAdviceInterceptor {

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    public TransactionAdviceInterceptor() {}

    @Bean
    @Value("${transacation.timeout: 10}")
    public TransactionInterceptor txAdvice(Integer transacationTimeout) {
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
        readOnlyTx.setReadOnly(true);
        readOnlyTx.setPropagationBehavior(4);
        RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
        requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        requiredTx.setPropagationBehavior(0);
        requiredTx.setTimeout(transacationTimeout);
        Map<String, TransactionAttribute> txMap = new HashMap();
        txMap.put("add*", requiredTx);
        txMap.put("save*", requiredTx);
        txMap.put("modify*", requiredTx);
        txMap.put("insert*", requiredTx);
        txMap.put("update*", requiredTx);
        txMap.put("del*", requiredTx);
        txMap.put("delete*", requiredTx);
        txMap.put("get*", readOnlyTx);
        txMap.put("query*", readOnlyTx);
        txMap.put("find*", readOnlyTx);
        txMap.put("select*", readOnlyTx);
        source.setNameMap(txMap);
        TransactionInterceptor txAdvice = new TransactionInterceptor(this.platformTransactionManager, source);
        return txAdvice;
    }

    @Bean
    @Value("${transacation.expression: execution( * com.das..*.service.*.*(..))}")
    public Advisor txAdviceAdvisor(String AOP_POINTCUT_EXPRESSION) {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, this.txAdvice((Integer)null));
    }
}
